<template>
  <div class="main">
    <div class="title">
      <a :href="this.courseId !== '' ? '/login/' + this.courseId : '/login'">登录</a>
      <span>·</span>
      <a class="active" :href="this.courseId !== '' ? '/register/' + this.courseId : '/register'">注册</a>
    </div>

    <div class="sign-up-container">
      <el-form ref="userForm" :model="params" status-icon>

        <el-form-item class="demo-ruleForm" prop="nickname"  :rules="[{ required: true, message: '请输入你的昵称', trigger: 'blur' }]">
          <div>
            <el-input prefix-icon="iconfont icon-user" type="text" placeholder="你的昵称" v-model="params.nickname"/>
          </div>
        </el-form-item>

        <el-form-item class="demo-ruleForm" prop="mobile" :rules="[{ required: true, message: '请输入手机号码', trigger: 'blur' },{validator: checkPhone, trigger: 'blur'}]">
          <div>
            <el-input prefix-icon="iconfont icon-phone" type="text" placeholder="手机号" v-model="params.mobile"/>
          </div>
        </el-form-item>

        <el-form-item class="demo-ruleForm " prop="code" :rules="[{ required: true, message: '请输入验证码', trigger: 'blur' }]">
          <div style="width: 100%;display: block;float: left;position: relative">
            <el-input prefix-icon="el-icon-s-comment"  type="text" placeholder="验证码" v-model="params.code"/>
          </div>
          <div class="btn" style="position:absolute;right: 16px;width: 40%;">
            <el-button type="text" :disabled="sending === false" href="javascript:" @click="getCodeInfo()" :value="codeTest" style="border: none;">{{codeTest}}</el-button>
          </div>
        </el-form-item>

        <el-form-item class="demo-ruleForm " prop="password" :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]">
          <div>
            <el-input prefix-icon="iconfont icon-password" type="password" placeholder="设置密码" v-model="params.password"/>
          </div>
        </el-form-item>

        <el-form-item class="demo-ruleForm" prop="password" :rules="[{ required: true, message: '请再次输入密码', trigger: 'blur' }, {validator: comparePassword, trigger: 'blur'}]">
          <div>
            <el-input prefix-icon="iconfont icon-password" type="password" placeholder="再次输入密码" v-model="againPassword"/>
          </div>
        </el-form-item>

        <div class="btn">
          <input type="button" class="sign-up-button" @click="registerUserInfo('userForm')" value="注册">
        </div>
        <p class="sign-up-msg">
          点击 “注册” 即表示您同意并愿意遵守谷粒学院
          <br>
          <a target="_blank" href="http://www.jianshu.com/p/c44d171298ce">用户协议</a>
          和
          <a target="_blank" href="http://www.jianshu.com/p/2ov8x3">隐私政策</a> 。
        </p>
      </el-form>
      <!-- 更多注册方式 -->
      <div class="more-sign">
        <h6>社交帐号直接注册</h6>
        <ul>
          <li><a id="weixin" class="weixin" target="_blank" href="http://huaan.free.idcfengye.com/api/ucenter/wx/login">
            <i class="iconfont icon-weixin"/></a></li>
          <li><a id="qq" class="qq" target="_blank" href="#"><i class="iconfont icon-qq"/></a></li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import 'assets/css/sign.css'
import 'assets/css/iconfont.css'

import registerApi from '@/api/register'

export default {
  layout: 'sign',
  data() {
    return {
      // 注册信息
      params: {
        mobile: '',
        code: '',
        nickname: '',
        password: ''
      },
      againPassword: '',
      sending: true, // 是否发送验证码
      second: 60, // 倒计时时间
      codeTest: '获取验证码',
      ruleForm: {
        pass: '',
      },
      courseId: '',
    }
  },
  created() {
    if (this.$route.params && this.$route.params.id) {
      this.courseId = this.$route.params.id
    }
  },
  methods: {
    // 倒计时
    timeDown() {
      let result = setInterval(() => {
        this.second--;
        this.codeTest = this.second + '秒后重新发送';
        if (this.second < 1) {
          clearInterval(result);
          this.sending = true;
          this.second = 60;
          this.codeTest = "获取验证码"
        }
      }, 1000)
    },
    // 获取验证码
    getCodeInfo() {
      this.sending = false
      this.timeDown()
      registerApi.getCode(this.params.mobile)
        .then(res => {
          this.$message({
            type: 'success',
            message: '验证码发送成功'
          })
        })
    },
    // 注册提交
    registerUserInfo(userForm) {
      // 验证表单如果都信息填入正确那么就调用后台接口
      this.$refs[userForm].validate(valid => {
        if (valid) {
          registerApi.registerUser(this.params)
            .then(res => {
              // if (res.data.code === 200){
                this.$message({
                  type: 'success',
                  message: '注册成功',
                })
              if (this.courseId === '') {
                this.$router.push({path: '/login'})
              } else {
                this.$router.push({path: '/login/' + this.courseId})
              }
            })
        } else {
          return false;
        }
      })

    },
    // 判断手机号是否正确
    checkPhone(rule, value, callback) {
      //debugger
      if (!(/^1[34578]\d{9}$/.test(value))) {
        return callback(new Error('手机号码格式不正确'))
      }
      return callback()
    },
    // 比较两次密码是否相同
    comparePassword(rule, value, callback) {
      if (this.againPassword !== this.params.password) {
        callback(new Error('两次输入密码不一致'));
      } else {
        callback();
      }
    }

  }
}
</script>
<style scoped>
#register >>> .el-form-item__content {
  line-height: 20px;
}

.main {
  padding-bottom: 10px;
}

.sign-up-msg {
  padding-top: 20px;
}
</style>
